package com.maoye.ossp.sys.base.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.maoye.ossp.common.web.BaseController;
import com.maoye.ossp.sys.base.entity.SpecificItems;
import com.maoye.ossp.sys.base.entity.SysDict;
import com.maoye.ossp.sys.base.entity.SysDictDetail;
import com.maoye.ossp.sys.base.service.ISysDictDetailService;
import com.maoye.ossp.sys.base.service.ISysDictService;
import com.maoye.ossp.sys.base.service.SpecificItemsService;
@Controller
@RequestMapping(value = "sys/base/spfItem/")
public class SpecificItemsController extends BaseController {
	
	@Resource
	SpecificItemsService service;
	
	@Resource
	ISysDictService   sysDictService;
	@Resource
	private ISysDictDetailService sysDictDetailService;
	@RequestMapping(value = "list")
	public String list(HttpServletRequest request, HttpServletResponse response,Model model) throws IOException {
		List<SpecificItems> list =service.getAll();
		model.addAttribute("list", list);
		return "/sys/base/spfItem/list";
	}

	@RequestMapping(value = "add")
	public String add(SpecificItems specificItems,HttpServletRequest request, HttpServletResponse response) throws IOException {
		
		return "/sys/base/spfItem/add";
	}
	
	@RequestMapping(value = "save")
	public String save(SpecificItems specificItems,HttpServletRequest request, HttpServletResponse response) throws IOException {
		if(specificItems.getId()!=null){
			service.update(specificItems);
		}else{
			specificItems.setId(idGenerator.getUId());
			service.create(specificItems);
		}
		return "redirect:/sys/base/spfItem/list";
	}
	
	@RequestMapping(value = "{id}/view")
	public String view(@PathVariable String id,HttpServletRequest request, HttpServletResponse response,Model model) throws IOException {
		SpecificItems specificItems=service.get(Long.parseLong(id));
		model.addAttribute("specificItems", specificItems);
		return "/sys/base/spfItem/view";
	}
	@RequestMapping(value = "{id}/edit")
	public String edit(@PathVariable String id,HttpServletRequest request, HttpServletResponse response,Model model) throws IOException {
		SpecificItems specificItems=service.get(Long.parseLong(id));
		model.addAttribute("specificItems", specificItems);
		return "/sys/base/spfItem/add";
	}
	@RequestMapping(value = "{id}/delete")
	public String delete(@PathVariable String id,HttpServletRequest request, HttpServletResponse response,Model model) throws IOException {
		service.remove(Long.parseLong(id));
		return "redirect:/sys/base/spfItem/list";
	}
	
	/***
	 * 获取临时付款项目
	 * @param request
	 * @param response
	 * @param model
	 * @throws IOException
	 * @author eric
	 * @data 2014年4月19日
	 */
	
	@RequestMapping(value = "getSpecificItems")
	public void getSpecificItems(HttpServletRequest request,HttpServletResponse response, Model model) throws IOException{
		response.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
		List<SpecificItems> speItemsList =service.getAll();
		StringBuffer sb = new StringBuffer();
		sb.append("[");
		sb.append("{\"itemNum\":\"\",\"itemName\":\"------请选择-----\"},");
		int count=0;
		for (SpecificItems specificItems : speItemsList) {
			count++;
			sb.append("{");
			sb.append("\"itemNum\":");
			sb.append("\""+specificItems.getItemNum()+"\"");
			sb.append(",");
			sb.append("\"itemName\":");
			sb.append("\""+specificItems.getItemName()+"\"");
			sb.append("}");
			if(speItemsList.size()!=count){
			  sb.append(",");
			}
		}
		sb.append("]");
		//out.write("[{\"provinceID\":\"110000\",\"province\":\"百丽\"},{\"provinceID\":\"120000\",\"province\":\"百丽2 \"}]");
		out.write(sb.toString());
		out.flush();
		out.close();
		
	}
	
	@RequestMapping(value = "getConditions")
	public void getConditions(HttpServletRequest request,HttpServletResponse response, Model model,String sptNum) throws IOException{
		response.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
		SpecificItems speItems =service.getSpecificItemsByItemNum(sptNum);
		HashMap<String, String> map = new HashMap<String, String>();
		map.put("dictType", speItems.getDicType());
		SysDict sysdict =sysDictService.getOne(map);
		
		HashMap<String, String> map2 = new HashMap<String, String>();
		map2.put("dictId", sysdict.getId().toString());
		List<SysDictDetail> dictList =sysDictDetailService.getList(map2);
		
		StringBuffer sb = new StringBuffer();
		sb.append("[");
		int count=0;
		for (SysDictDetail dict : dictList) {
			count++;
			sb.append("{");
			sb.append("\"key\":");
			sb.append("\""+dict.getKey()+"\"");
			sb.append(",");
			sb.append("\"value\":");
			sb.append("\""+dict.getValue()+"\"");
			sb.append("}");
			if(dictList.size()!=count){
			  sb.append(",");
			}
		}
		sb.append("]");
		//out.write("[{\"provinceID\":\"110000\",\"province\":\"百丽\"},{\"provinceID\":\"120000\",\"province\":\"百丽2 \"}]");
		out.write(sb.toString());
		out.flush();
		out.close();
		
	}
}
